package com.cysion.study.uibox.dialog

import android.app.Activity
import android.app.AlertDialog
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.cysion.study.R
import com.cysion.study.ktbox.utils.CLog
import java.util.*

/**
 * Created by cysion on 2018\2\11 0011.
 */
typealias OnTypeSearchClickListener = (type: Int, start: String, end: String) -> Unit

const val SEARCHCONFIRM = 1
const val SEARCHCANCEL = 0


object AlertSearch {
    private var mDialog: AlertDialog? = null


    //初始化对话框，并弹出；上层弹窗时不调用；上层 定制弹窗 时可使用
    fun setup(
        src: Activity,
        layoutId: Int,
        widthMulti: Float,
        cancelable: Boolean,
        clickListener: OnTypeSearchClickListener?,
        dim: Float = 0f
    ): View {
        mDialog?.dismiss()
        val builder = AlertDialog.Builder(src)
        val inflater = LayoutInflater.from(src)
        val view = inflater.inflate(layoutId, null)
        val dialog = builder.create()
        val window = dialog.window
        dialog.show()
        dialog.setCanceledOnTouchOutside(cancelable)
        dialog.setCancelable(cancelable)
        //摆脱token的限制，注意清单文件alert权限
        val p = window!!.attributes // 获取对话框当前的参数值
        window.decorView.setBackgroundColor(0X00000000)
        p.width = (src.resources.displayMetrics.widthPixels * widthMulti).toInt()
        window.attributes = p
        window.setDimAmount(dim)
        window.setBackgroundDrawable(null)
        dialog.window!!.setContentView(view)//自定义布局应该在这里添加，要在dialog.show()的后面
        dialog.window!!.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        dialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
        mDialog = dialog
        return view
    }


    //关闭对话框
    fun close() {
        mDialog?.dismiss()
        mDialog = null
    }


    //加载框
    fun loading(src: Activity) {
        src.runOnUiThread {
            setup(src, R.layout.dialog_loading, 1f, true, null)
        }
    }




    var start: String = ""
    var end: String = ""
    lateinit var cal: Calendar
    var year: Int = 0
    var month: Int = 0
    var day: Int = 0
    var hour: Int = 0
    var minute: Int = 0


    //搜索
    const val alipay = 1
    const val wechat = 2
    const val blance = 3
    fun search(
        ac: Activity,
        title: String,
        clickListener: OnTypeSearchClickListener?
    ) {
        val view = setup(ac, R.layout.dialog_search, 0.8f, true, clickListener, dim = 0.3f)
        var iv_start = view.findViewById<ImageView>(R.id.iv_start)
        var iv_end = view.findViewById<ImageView>(R.id.iv_end)
        var tv_start = view.findViewById<TextView>(R.id.tv_start)
        var tv_end = view.findViewById<TextView>(R.id.tv_end)

        //获取日历的一个对象
        cal = Calendar.getInstance()
        //获取年月日时分秒的信息
        year = cal.get(Calendar.YEAR)
        //month从0开始计算(一月month = 0)
        month = cal.get(Calendar.MONTH) + 1
        day = cal.get(Calendar.DAY_OF_MONTH)
        hour = cal.get(Calendar.HOUR_OF_DAY)
        minute = cal.get(Calendar.MINUTE)

        iv_start.setOnClickListener {
            dialog(ac,tv_start,"1")
        }
        iv_end.setOnClickListener {
            dialog(ac,tv_end,"2")
        }
        view.findViewById<TextView>(R.id.tv_confirm).setOnClickListener {
            mDialog?.dismiss()
            clickListener?.invoke(CONFIRM, start, end)
        }
    }

    private fun dialog(ac: Activity,view: TextView,type:String) {
        val timeDialog = TimePickerDialog(ac,
            TimePickerDialog.OnTimeSetListener { tp, hourOfDay, minute ->
                if ("1" == type) {
                    start =
                        start + "  " + (if (hourOfDay < 10) "0$hourOfDay" else hourOfDay) + ":" + (if (minute < 10) "0$minute" else minute) + ":" + "00"
                    view.setText(start)
                } else if ("2" == type) {
                    end =
                        end + "  " + (if (hourOfDay < 10) "0$hourOfDay" else hourOfDay) + ":" + (if (minute < 10) "0$minute" else minute) + ":" + "00"
                    view.setText(end)
                }
                CLog.i("时间=$start--end=$end")
            }, hour, minute, true
        )
        timeDialog.setTitle("请选择时间")
        timeDialog.show()


        val dateDialog = DatePickerDialog(
            ac,
            DatePickerDialog.OnDateSetListener { dp, year, month, dayOfMonth ->
                CLog.i("年ssss=" + year + "--月=" + (if (month + 1 < 10) "0" + (month + 1) else month + 1) + "---日=" + if (dayOfMonth < 10) "0$dayOfMonth" else dayOfMonth)
                //2017-12-04 12：00：00
                if ("1" == type) {
                    start =
                        year.toString() + "-" + (if (month + 1 < 10) "0" + (month + 1) else month + 1) + "-" + if (dayOfMonth < 10) "0$dayOfMonth" else dayOfMonth
                } else if ("2" == type) {
                    end =
                        year.toString() + "-" + (if (month + 1 < 10) "0" + (month + 1) else month + 1) + "-" + if (dayOfMonth < 10) "0$dayOfMonth" else dayOfMonth
                }
            }, year, cal.get(Calendar.MONTH), day
        )
        dateDialog.setTitle("请选择日期")
        dateDialog.show()
    }

}
